﻿package src.uipackage.ui.graphics {
	
	import flash.display.CapsStyle;
	import flash.display.LineScaleMode;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	
	/**
	* Creates graphic display for UI combo box.
	* @author Devon O.
	*/
	public class ComboBoxGraphic extends Sprite {
		
		private var _w:int;
		private var _h:int;
		private var _button:SimpleButton;
		private var _arrow:Sprite;
		
		public function ComboBoxGraphic(width:int, height:int):void {
			_w = width;
			_h = height;
			
			drawBox();
			addButton();
			addArrow();
		}
		
		private function drawBox():void {
			graphics.lineStyle(1, 0x848484, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			graphics.moveTo(0, _h - 1);
			graphics.lineTo(0, 0);
			graphics.lineTo(_w, 0);
			graphics.lineStyle(1, 0xE3DDDD, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			graphics.moveTo(_w, 1);
			graphics.lineTo(_w, _h - 1);
			graphics.lineStyle(1, 0xE9E9E9, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			graphics.moveTo(_w, _h);
			graphics.lineTo(0, _h);
			graphics.lineStyle(1, 0xE3DDDD, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			graphics.moveTo(1, 1);
			graphics.lineTo(_w - 1, 1);
			graphics.lineTo(_w - 1, _h - 1);
			graphics.lineTo(1, _h - 1);
			graphics.lineTo(1, 1);
			cacheAsBitmap = true;
		}
		
		private function addButton():void {
			var btnSize:int = _h - 4;
			
			var upstate:Sprite = new Sprite();
			upstate.graphics.beginFill(0xFFFFFF);
			upstate.graphics.lineStyle(1, 0xE3DDDD, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			upstate.graphics.moveTo(0, 0);
			upstate.graphics.lineTo(btnSize - 1, 0);
			upstate.graphics.lineTo(btnSize - 1, btnSize - 1);
			upstate.graphics.lineTo(0, btnSize - 1);
			upstate.graphics.lineTo(0, 0);
			upstate.graphics.endFill();
			// shadow
			upstate.graphics.lineStyle(1, 0x848484, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			upstate.graphics.moveTo(0, btnSize);
			upstate.graphics.lineTo(btnSize, btnSize);
			upstate.graphics.lineTo(btnSize, 0);
			upstate.cacheAsBitmap = true;
			
			var overstate:Sprite = new Sprite();
			overstate.graphics.beginFill(0xF5F5F5);
			overstate.graphics.lineStyle(1, 0xE3DDDD, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			overstate.graphics.moveTo(0, 0);
			overstate.graphics.lineTo(btnSize - 1, 0);
			overstate.graphics.lineTo(btnSize - 1, btnSize - 1);
			overstate.graphics.lineTo(0, btnSize - 1);
			overstate.graphics.lineTo(0, 0);
			overstate.graphics.endFill();
			// shadow
			overstate.graphics.lineStyle(1, 0x848484, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			overstate.graphics.moveTo(0, btnSize);
			overstate.graphics.lineTo(btnSize, btnSize);
			overstate.graphics.lineTo(btnSize, 0);
			overstate.cacheAsBitmap = true;
			
			var downstate:Sprite = new Sprite();
			downstate.graphics.beginFill(0xF5F5F5);
			downstate.graphics.lineStyle(1, 0x848484, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			downstate.graphics.moveTo(0, btnSize - 1);
			downstate.graphics.lineTo(0, 0);
			downstate.graphics.lineTo(btnSize - 1, 0);
			downstate.graphics.lineStyle(1, 0xE3DDDD, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			downstate.graphics.lineTo(btnSize - 1, btnSize - 1);
			downstate.graphics.lineTo(0, btnSize - 1);
			downstate.graphics.endFill();
			// shadow
			downstate.graphics.lineStyle(1, 0xE3DDDD, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
			downstate.graphics.moveTo(0, btnSize);
			downstate.graphics.lineTo(btnSize, btnSize);
			downstate.graphics.lineTo(btnSize, 0);
			downstate.cacheAsBitmap = true;
			
			var hitstate:Sprite = new Sprite();
			hitstate.graphics.beginFill(0x000000);
			hitstate.graphics.moveTo((btnSize + 1) - _w, -2);
			hitstate.graphics.lineTo(btnSize + 1, -2);
			hitstate.graphics.lineTo(btnSize + 1, btnSize + 2);
			hitstate.graphics.lineTo((btnSize + 1) - _w, btnSize + 2);
			hitstate.graphics.endFill();
			hitstate.cacheAsBitmap = true;
			
			_button = new SimpleButton(upstate, overstate, downstate, hitstate);
			
			_button.x = _w - btnSize - 1;
			_button.y = 2;
			addChild(_button);
		}
		
		private function addArrow():void {
			_arrow = new Sprite();
			_arrow.graphics.beginFill(0x282828);
			_arrow.graphics.moveTo(0, 0);
			_arrow.graphics.lineTo(6, 0);
			_arrow.graphics.lineTo(3, 3);
			_arrow.graphics.lineTo(0, 0);
			_arrow.graphics.endFill();
			_arrow.cacheAsBitmap = true;
			
			_arrow.x = int((_button.x + (_button.width / 2)) - (_arrow.width / 2));
			_arrow.y = int((_h / 2) - (_arrow.height / 2));
			addChild(_arrow);
		}
	}
}